package server_center

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/server_center"
	server_centerReq "github.com/flipped-aurora/gin-vue-admin/server/model/server_center/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SysLogApi struct {
}

var sysLogService = service.ServiceGroupApp.Server_centerServiceGroup.SysLogService
var userService = service.ServiceGroupApp.SystemServiceGroup.UserService
var projectService = service.ServiceGroupApp.BcServiceGroup.BcProjectService

// CreateSysLog 创建sysLog表
// @Tags SysLog
// @Summary 创建sysLog表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body server_center.SysLog true "创建sysLog表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /sysLog/createSysLog [post]
func (sysLogApi *SysLogApi) CreateSysLog(c *gin.Context) {
	var sysLog server_center.SysLog
	err := c.ShouldBindJSON(&sysLog)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 根据项目名称查询项目编号
	data1, err := projectService.GetBcProject(uint(sysLog.ProjectId))
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	sysLog.ProjectName = data1.ProjectName
	// 根据用户名查出用户ID 还有用户上级的ID和上级的名字
	data, err := userService.GetUserInfoByName(sysLog.SysUserName)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	*sysLog.SysUserId = int(data.ID)
	data, err = userService.GetUserInfoByCode(data.SettleOffice, 8881)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	*sysLog.SuperiorsId = int(data.ID)
	sysLog.SuperiorsName = data.Username
	//if *sysLog.SuperiorsId == *sysLog.SysUserId {
	//	lobal.GVA_LOG.Error("代理用户无法创建!", zap.Error(err))
	//	response.FailWithMessage("代理用户无法创建", c)
	//	return
	//}

	if err := sysLogService.CreateSysLog(&sysLog); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteSysLog 删除sysLog表
// @Tags SysLog
// @Summary 删除sysLog表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body server_center.SysLog true "删除sysLog表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /sysLog/deleteSysLog [delete]
func (sysLogApi *SysLogApi) DeleteSysLog(c *gin.Context) {
	var sysLog server_center.SysLog
	err := c.ShouldBindJSON(&sysLog)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := sysLogService.DeleteSysLog(sysLog); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteSysLogByIds 批量删除sysLog表
// @Tags SysLog
// @Summary 批量删除sysLog表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除sysLog表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /sysLog/deleteSysLogByIds [delete]
func (sysLogApi *SysLogApi) DeleteSysLogByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := sysLogService.DeleteSysLogByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateSysLog 更新sysLog表
// @Tags SysLog
// @Summary 更新sysLog表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body server_center.SysLog true "更新sysLog表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /sysLog/updateSysLog [put]
func (sysLogApi *SysLogApi) UpdateSysLog(c *gin.Context) {
	var sysLog server_center.SysLog
	err := c.ShouldBindJSON(&sysLog)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := sysLogService.UpdateSysLog(sysLog); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindSysLog 用id查询sysLog表
// @Tags SysLog
// @Summary 用id查询sysLog表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query server_center.SysLog true "用id查询sysLog表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /sysLog/findSysLog [get]
func (sysLogApi *SysLogApi) FindSysLog(c *gin.Context) {
	var sysLog server_center.SysLog
	err := c.ShouldBindQuery(&sysLog)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if resysLog, err := sysLogService.GetSysLog(sysLog.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"resysLog": resysLog}, c)
	}
}

// GetSysLogList 分页获取sysLog表列表
// @Tags SysLog
// @Summary 分页获取sysLog表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query server_centerReq.SysLogSearch true "分页获取sysLog表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sysLog/getSysLogList [get]
func (sysLogApi *SysLogApi) GetSysLogList(c *gin.Context) {
	var pageInfo server_centerReq.SysLogSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	data, err := utils.GetClaims(c)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}
	list := make([]string, 0)
	switch data.AuthorityId {
	case 8881:
		data1, err := userService.GetUserListByCode(data.SettleOffice)
		if err != nil {
			response.FailWithMessage("获取失败", c)
			return
		}

		for i := 0; i < len(data1); i++ {
			list = append(list, data1[i].Username)
		}
	case 1:
		list = append(list, data.Username)
	}
	if list, total, err := sysLogService.GetSysLogInfoList(pageInfo, list); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
